package com.shanghaionstar.mro.VechileStatusCheck;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.onstar.cn.common.util.Constants;
import com.shanghaionstar.carmanage.CarManageActivity;
import com.shanghaionstar.carmanage.iview.ICarStatusView;
import com.shanghaionstar.carmanage.presenter.CarStatusPresenter;
import com.shanghaionstar.mro.MrOActivity;
import com.shanghaionstar.utils.BusinessUtil;

/**
 * 小O车况信息查询：
 * <p/>
 * *************************************
 * 1：车况查询: 跳转爱车管理页面，并自动下拉刷新车况
 * ************************************
 * （以下行为自动发送车况刷新操作）
 * 2：胎压查询
 * 3：油量查询
 * 4：机油寿命查询
 * 5：里程数查询
 * *************************************
 * <p/>
 * 使用说明：
 * <p/>
 * new MrOVechileStatusQueryUtil(mrOActivity).query("conditionquery");
 * <p/>
 * <p/>
 * Created by Yonglang Hong on 2016/12/30.
 */

public class MrOVechileStatusQueryUtil {

    private CarStatusPresenter presenter;

    private MrOActivity mrOActivity;

    /**
     * 车况查询
     */
    public final static String CONDITION_QUERY = "conditionquery";
    /**
     * 胎压查询
     */
    public final static String PRESSURE_QUERY = "pressurequery";
    /**
     * 油量查询
     */
    public final static String OIL_QUERY = "oilquery";
    /**
     * 机油寿命查询
     */
    public final static String OILLIFE_QUERY = "oillifequery";
    /**
     * 里程查询
     */
    public final static String MILEAGE_QUERY = "mileagequery";

    public MrOVechileStatusQueryUtil(MrOActivity mrOActivity) {
        this.mrOActivity = mrOActivity;
    }

    public void release(){
        if (presenter != null) {
            presenter.finish();
            presenter = null;
        }
    }

    /**
     * 车况查询:
     * 根据小O返回类型查询
     */
    public void query(String type) {
        if (TextUtils.isEmpty(type))
            return;
        MrOVehicleStatusQueryRecordHelp.getRecordHelpInstance(mrOActivity.getApplicationContext()).setOrderType(type);
        switch (type.toLowerCase()) {
            case CONDITION_QUERY:
                gotoCarStatusPage();
                break;
            case PRESSURE_QUERY:
            case OIL_QUERY:
            case OILLIFE_QUERY:
            case MILEAGE_QUERY:
                refreshVechileData();
                break;
        }
    }

    /**
     * 校验当前指令是否可执行
     */
    public boolean validateDataRefresh() {
        if (BusinessUtil.checkInProgressCommand(mrOActivity, null, true)) {
            return false;
        }
        return BusinessUtil.checkDataRefreshAuth(mrOActivity, Constants.ENTITLEMENT_M01DATA_REFRESH);
    }

    /**
     * 刷新车况
     */
    private void refreshVechileData() {
        if (validateDataRefresh()) {
            getPresenter().refreshVechileData();
        }
    }

    /**
     * 跳转车辆管理页面
     */
    private void gotoCarStatusPage() {
        Intent intent = new Intent(mrOActivity, CarManageActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString(CarManageActivity.FROM, CarManageActivity.MRO);
        intent.putExtras(bundle);
        mrOActivity.startActivity(intent);
    }

    private CarStatusPresenter getPresenter() {
        if (presenter == null) {
            presenter = new CarStatusPresenter(mrOActivity.getScheduler(), mrOActivity);
            presenter.setStatusView(carStatusView);
        }
        return presenter;
    }

    private ICarStatusView carStatusView;

    public void setCarStatusView(ICarStatusView carStatusView) {
        this.carStatusView = carStatusView;
    }

}
